//
//  WBNumberView.m
//  365YP
//
//  Created by 文彬 on 2017/3/30.
//  Copyright © 2017年 文彬. All rights reserved.
//

#import "WBNumberView.h"

@interface WBNumberView()

{
    UIButton *minusBtn;
    UIButton *addBtn;
    UITextField *numTxtFied;
   
}

@end

@implementation WBNumberView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initControl];
    }
    
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self initControl];
}

- (void)initControl
{
    _currentNum = 0;
    _minCount = 1;
    
    minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [minusBtn setBackgroundImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
    [self addSubview:minusBtn];
    [minusBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@5);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-5);
        make.width.equalTo(@(self.frame.size.height-10));
    }];
    [minusBtn buttonClick:^{
       
        if (_currentNum==0)
        {
            return ;
        }
        
        if ((_currentNum==1&&!self.zearoModel&&!self.showZearoModel)||(self.showZearoModel&&_currentNum==0))
        {
            
//            if ([self.delegate respondsToSelector:@selector(minusClicked:)])
//            {
//                [self.delegate minusClicked:self];
//            }
            return ;
        }

        if (self.currentNum==self.minCount)
        {
            _currentNum = 0;
        }
        else
        {
            _currentNum--;
        }
       
        [self setSelect:_currentNum];
        
        if ([self.delegate respondsToSelector:@selector(minusClicked:)])
        {
            [self.delegate minusClicked:self];
        }
    }];
    
    addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
//    [addBtn setTitle:@"+" forState:UIControlStateNormal];
//    [addBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    addBtn.layer.borderWidth = 0.5;
//    addBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    addBtn.layer.cornerRadius = self.frame.size.height/2;
    [self addSubview:addBtn];
    [addBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-5);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-5);
        make.width.equalTo(@(self.frame.size.height-10));
    }];
    [addBtn buttonClick:^{
       
        if (minusBtn.hidden||_currentNum==0) //第一次点击加号 基数变为最低数量
        {
            _currentNum+=self.minCount;
        }
        else
        {
            _currentNum++;
        }
     
        [self setSelect:_currentNum];
        
        if ([self.delegate respondsToSelector:@selector(addClicked:)])
        {
            [self.delegate addClicked:self];
        }
        
//        minusBtn.hidden = NO;
//        numTxtFied.hidden = NO;
    }];
    
    numTxtFied = [[UITextField alloc]init];
    numTxtFied.font = [UIFont systemFontOfSize:15];
    numTxtFied.textAlignment = NSTextAlignmentCenter;
    numTxtFied.textColor = [UIColor blackColor];
    numTxtFied.keyboardType = UIKeyboardTypeNumberPad;
    numTxtFied.text = @"1";
    numTxtFied.adjustsFontSizeToFitWidth = YES;
    numTxtFied.enabled = NO;
    [self addSubview:numTxtFied];
    [numTxtFied makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(minusBtn.right);
        make.right.equalTo(addBtn.left);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    
    //在列表时 容易误触发到详情  加个手势拦截一下
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)];
    [self addGestureRecognizer:tap];
}
- (void)test
{
    
}

- (void)setSelect:(int)index
{
    _currentNum = index;
    numTxtFied.text = [NSString stringWithFormat:@"%d",index];
    if(self.zearoModel&&index==0)
    {
        minusBtn.hidden = YES;
        numTxtFied.hidden = YES;
    }
    else
    {
        minusBtn.hidden = NO;
        numTxtFied.hidden = NO;
    }
}

- (void)setNumberColor:(UIColor*)color
{
    numTxtFied.textColor = color;
}

- (void)setCanInputNum:(BOOL)canInputNum
{
    _canInputNum = canInputNum;
    numTxtFied.enabled = canInputNum;
}

@end
